/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fachada;

import dominio.Persona;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author rodrifonsi
 */
@Stateless
public class PersonaDAOBean implements PersonaDAO {

    @PersistenceContext(unitName = "EnterpriseApplication1-ejbPU")
    private EntityManager em;

    public void create(Persona p) {
        try {
            em.persist(p);
        } catch (Exception e) {
            Logger.getLogger(PersonaDAOBean.class.getName()).log(Level.SEVERE, null, e);
        }
    }

    public Persona getPersona(Integer idPersona) {
        Persona u = new Persona();
        u = em.find(Persona.class, idPersona);
        return u;
    }

    public List<Persona> getAllPersona() {
        Query q = em.createQuery("SELECT a FROM Persona a");
        List<Persona> tr = q.getResultList();
        return tr;

    }

    public void update(Persona p) {
        try {
            em.merge(p);
        } catch (Exception e) {
            Logger.getLogger(PersonaDAOBean.class.getName()).log(Level.SEVERE, null, e);
        }
    }

    public void delete(Persona p) {
        try {
            Persona p1 = this.getPersona(p.getId());
            em.remove(p1);
        } catch (Exception e) {
            Logger.getLogger(PersonaDAOBean.class.getName()).log(Level.SEVERE, null, e);
        }
    }
}
